package edu.gatech.cs2340.sharks.fms;

/**
 * LoginActivity.java
 * @author Monica Shum
 * 
 * Activity for all clients to log in. Displays 2 EditTexts for entering username and password, then a button to log in and a button for creating a new account. 
 */

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import edu.gatech.cs2340.sharks.fms.data.*;

public class LoginActivity extends Activity
{
	/**
	 * ProgressBar object used when the AsyncTask is executed. Mainly to show the user that the app is communicating and ensure that it is working.
	 * Necessarily public since it needs to be accessed from inner class.
	 */
	public ProgressBar progressCircle;
	
	/**
	 * The current instance of the activity, used to make sure there is only one instance of this activity running at a time.
	 */
	private static Activity instance = null;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		if (instance != null) instance.finish();
		instance = this;

		progressCircle = (ProgressBar) findViewById(R.id.progressCircle);
		TextView register_status = (TextView) findViewById(R.id.loginstatus);
		Intent intent = getIntent();
		String success_message = intent.getStringExtra(RegisterActivity.SUCCESS_MESSAGE);
		register_status.setText(success_message);
	}
	
	/**
	 * Called when user clicks the Sign In button. 
	 * 
	 * @param view a reference to the widget that was clicked
	 */
	public void tryLogin(View view)
	{
		final TextView status = (TextView) findViewById(R.id.loginstatus);
		status.setText("");
		final EditText username = (EditText) findViewById(R.id.username);
		String userID = username.getText().toString();
		final EditText password = (EditText) findViewById(R.id.password);
		String userPASS = password.getText().toString();

		/**
		 * Inner class defining the AsyncTask responsible for handling the Progress Bar Spinner while running the lengthy log in function in a back thread.
		 * 
		 * @author: Wai T. Yeung
		 */	
		class LoginTask extends AsyncTask<String , Void, Packet<User>>
		{
			@Override
			protected void onPreExecute()
			{
				super.onPreExecute();
				progressCircle.setVisibility(View.VISIBLE);
			}
			
			/**
			 * Automatically called after onPreExecute and before onPostExecute. Does sets up and execute actual back thread operations.
			 * 
			 * @param strings an array of indefinite size containing strings from the parameters of the class.
			 * @return a Packet of User objects returned by Database Manager's login function.1
			 */	
			protected Packet<User> doInBackground(String... strings) {
				Packet<User> result = DBMgr.login(strings[0], strings[1]);
				return result;
			}
			
			/**
			 * Automatically called after doInBackgroun is executed.
			 * 
			 * @param result a Packet<User> gotten from doInBackground containing the info of the log in session and its status. 
			 */	
			@Override
			protected void onPostExecute(Packet<User> result)
			{
				super.onPostExecute(result);
				
				if (result.status.code() == edu.gatech.cs2340.sharks.fms.data.Status.Code.OK) 
				{
					Intent toHome;
					if (result.value.role() == User.Role.CLIENT) toHome = new Intent(getApplicationContext(), HomeActivity.class); 
					else toHome = new Intent(getApplicationContext(), AdminHomeActivity.class);
					((EditText)username).setText("");
					((EditText)password).setText("");
					startActivity(toHome);
				}
				else
				{
					status.setText(result.status.toString());
					password.setText("");
					password.requestFocus();
				}
				
				progressCircle.setVisibility((View.INVISIBLE));
			}
		}

		new LoginTask().execute(userID, userPASS);
	}
	
	/**
	 * Called when user clicks the Try Register button.
	 * 
	 * @param view a reference to the widget that was clicked
	 */		
	public void tryRegister(View view){
		Intent toRegister = new Intent(this, RegisterActivity.class);
		startActivity(toRegister);
	}
}